package com.stars.home.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.base.MBActivity
import com.stars.home.databinding.ActivityErrorBinding
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel


/**
 *
 *on 2020/11/10
 */
class CustomErrorActivity : MBActivity<BViewModel<BRepository>, ActivityErrorBinding>(){


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        var config: CaocConfig? =null
        try {
            binding.errorDetails.text=CustomActivityOnCrash.getStackTraceFromIntent(intent)
             config = CustomActivityOnCrash.getConfigFromIntent(intent)

        }catch (e:Exception){
        }

        binding.restartCopy.onClick {
            //复制错误信息

            val cm: ClipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            // 创建普通字符型ClipData
            val mClipData = ClipData.newPlainText(null, binding.errorDetails.text)
            // 将ClipData内容放到系统剪贴板里。
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData)
            toast("错误信息已复制到粘贴板")
        }


        if (config == null) {
            finish()
            return
        }

        if (config.isShowRestartButton && config.restartActivityClass != null) {
            binding.restartButton.text="重启应用"

            binding.restartButton.onClick {
                CustomActivityOnCrash.restartApplication(this@CustomErrorActivity, config)
            }


        } else {

            binding.restartButton.onClick {
                CustomActivityOnCrash.closeApplication(this@CustomErrorActivity, config)
            }

        }


    }


    override fun title(): String? {
        return "错误信息"

    }

    override fun binding(): ActivityErrorBinding {
        return ActivityErrorBinding.inflate(layoutInflater)
    }
}
